<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\AiPro\Service;

use App\AiPro\Mapper\AiproWebsiteMapper;
use Mine\Abstracts\AbstractService;

/**
 * 导航页网站服务类
 */
class AiproWebsiteService extends AbstractService
{
    /**
     * @var AiproWebsiteMapper
     */
    public $mapper;

    public function __construct(AiproWebsiteMapper $mapper)
    {
        $this->mapper = $mapper;
//        $this->clientFactory = $clientFactory;
    }


    public function getWebsite()
    {

        $html = file_get_contents('/data/server/storage/bb.html');

        $card_html = QueryList::html($html)->rules([  //设置DOM解析规则
            'id' => ['.tab-pane', 'id'],
            'html' => ['', 'html'],
        ])->range('.test11')->query()->getData();

        $res = [];
        $sum  = 0;
        foreach ($card_html as $item) {
            $arr = explode('-', $item['id']);

            $ins_data = QueryList::html($item['html'])->rules([  //设置DOM解析规则
                'link' => ['a', 'data-url'],
                'title' => ['.text-sm', 'text'],
                'desc' => ['.text-xs', 'text'],
                'img' => ['.unfancybox', 'src'],
            ])->range('.url-body')->query()->getData();
            var_dump(count($ins_data));
            $sum += count($ins_data);
            foreach ($ins_data as $key => $datum) {
                $datum['desc'] = preg_replace('/[\r\n]/', '', $datum['desc']);
                $datum['desc'] = str_replace(' ', '', $datum['desc']);
                $datum['home_category_id'] = $arr[2];
                $res[] = $datum;
            }
        }

        foreach ($res as $re){
            AiproWebsite::query()->insert($re);
        }
        var_dump('============');
        return $res;

    }



}